package outman.weibo;

import outman.weibo.ui.WeiboDialog;
import android.Manifest;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.webkit.CookieSyncManager;

public class WeiboClient {

    public static final String CONSUMER_KEY = "2984646977";
    public static final String CONSUMER_SECRET = "89a857cc00d4a26ba3ea45d89de4fbcc";

    
    public static final String ACCESS_TOKEN_URL = "https://api.weibo.com/oauth2/access_token";
    public static final String AUTHORIZE_URL = "https://api.weibo.com/oauth2/authorize";

    public static final String REDIRECT_URI = "http://127.0.0.1";

//    public static final String CALLBACK_SCHEME = "outman";
//    public static final String CALLBACK_HOST = "weibo";
//    public static final String CALLBACK_URL = CALLBACK_SCHEME + "://"
//	    + CALLBACK_HOST;
    
    public static final String ACCESS_TOKEN = "access_token";

    private static String accessToken;
    private static long expiresIn;

    private static WeiboClient client;

    public static WeiboClient getInstance() {
	if (null == client) {
	    client = new WeiboClient();
	}
	return client;
    }

    public long getExpiresIn() {
	return expiresIn;
    }
    
    public void setExipresIn(long expiresIn)
    {
	WeiboClient.expiresIn = expiresIn;
    }

    /**
     * 将String形式的ExpiresIn转换成long，以便于进行比较判断
     * @param expiresIn
     */
    public void setExpiresIn(String expiresIn) {
	if(null != expiresIn && !expiresIn.equals("0"))
	{
	    setExipresIn(System.currentTimeMillis() + Integer.parseInt(expiresIn) * 1000);
	}
    }

    public String getAccessToken() {
	return accessToken;
    }

    public void setAccessToken(String accessToken) {
	WeiboClient.accessToken = accessToken;
    }

    public void authorize(Context context, WeiboDialogListener listener) {
	startDialogAuth(context, listener);
    }

    private void startDialogAuth(Context context, WeiboDialogListener listener) {

	WeiboParameter parameter = new WeiboParameter();
	parameter.add("client_id", CONSUMER_KEY);
	parameter.add("response_type", "token");
	parameter.add("redirect_uri", REDIRECT_URI);
	parameter.add("display", "mobile");
	
	if(isSessionValid())
	{
	    parameter.add(ACCESS_TOKEN, accessToken);
	}
	
	String url = AUTHORIZE_URL + parameter.toGetParam();

	if (context.checkCallingOrSelfPermission(Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) {
	    showAlert(context, "error", "network not available!");
	} else {
	    new WeiboDialog(context, url, listener).show();
	}
    }

    private static void showAlert(Context context, String title, String message) {
	Builder builder = new Builder(context);
	builder.setTitle(title);
	builder.setMessage(message);
	builder.create().show();
    }
    
    public boolean isSessionValid()
    {
	if(null != accessToken && 0 != accessToken.length())
	{
	    return (WeiboClient.getInstance().getExpiresIn() > System.currentTimeMillis());
	}
	
	return false;
    }

}
